import Dialog from '@vant/weapp/dialog/dialog'

Page({
  data: {
    list: [],
  },

  async getList() {
    const res = await wx.http({ url: '/room' })
    this.setData({ list: res.data })
  },

  async swipeClose(e) {
    // position 点击的位置（左、右）
    // instance van-swipe-cell实例
    const { position, instance } = e.detail
    try {
      if (position === 'right') {
        await Dialog.confirm({
          title: '是否删除已绑定房屋？',
        })
        // 只要能走下来，说明点的是确定
        console.log(e.mark.id)
        await wx.http({ method: 'delete', url: '/room/' + e.mark.id })
        // 只要能走到这里，说明删除成功
        wx.utils.toast('删除成功')
        await this.getList()
      }
    } finally {
      // 关闭滑动单元格
      instance.close()
    }
  },

  goDetail(e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },

  onShow() {
    this.getList()
  },
})
